#pragma once

#include <string>
#include "UIEvent.h"
#include "UIModule.h"
#include "CommonUtil.h"
#include "SpeechError.h"

#define EVENT_IN_START_RECORDING	"start_recording"
#define EVENT_IN_STOP_RECORDING		"stop_recording"
#define EVENT_IN_USER_VOICE_DATA	"user_voice_data"
#define EVENT_IN_BATCH_SR_TEST		"batch_sr_test"
#define EVENT_IN_BATCH_SR_NLU_TEST	"batch_sr_nlu_test"
#define EVENT_IN_TTS_TEXT			"tts_text"
#define EVENT_IN_TTS_STOP			"stop_tts"
#define EVENT_IN_PARAMETER_UPDATE	"sr_tts_parameters"
#define EVENT_IN_CONTEXT_UPDATE		"context_update"

#define EVENT_OUT_START_RECORDING	"start_recording"
#define EVENT_OUT_END_RECORDING		"end_recording"
#define EVENT_OUT_TTS_VOICE_DATA	"tts_voice_data"
#define EVENT_OUT_TTS_PLAY_DONE		"tts_play_done"
#define EVENT_OUT_SR_ERROR			"sr_error"
#define EVENT_OUT_TTS_ERROR			"tts_error"
#define EVENT_OUT_SR_NLU_RESULT		"sr_nlu_result"
#define EVENT_OUT_SNR_LEVEL			"snr_level"

static void sendSpeechOutboundEvent(int sessionID, int pairID, const char* type, std::string value)
{
	UIEvent event;
	event.setSender(MODULE_SpeechManager);
	if (equalsIgnoreCase(type, EVENT_OUT_SR_NLU_RESULT)) 
		event.setReciever(MODULE_LanguageInterpretation);
	else
		event.setReciever(MODULE_DialogueManager);

	event.setSessionID(sessionID);
	event.setPairID(pairID);

	event.setType(type);
	event.setTimeStamp(0);
	event.setValue(value);

	UIEventRouter::getInstance()->sendEvent(&event);
}

static std::string serializeTTSVoiceData(int packNo, bool isLastPackage, char* data, int datalen)
{
	char* buf = new char[datalen];
	strncpy(buf, data, datalen);
	std::string strData(buf);

	std::string jsonString = "{";
	jsonString += "\"pkg_no\":" + toStr(packNo) + ",";
	jsonString += "\"last_pkg\":" + (isLastPackage)?"true,":"false,";
	jsonString += "\"data\":\"" + strData + "\"";
	jsonString += "}";
	return jsonString;
}

static std::string serializeSRError(SRError error)
{
	std::string jsonString = "{";
	jsonString += "\"code\":\"" + getSRErrorCode(error) + "\",";
	jsonString += "\"description\":\"" + getSRErrorDesc(error) + "\"";
	jsonString += "}";
	return jsonString;
}

static std::string serializeTTSError(TTSError error)
{
	std::string jsonString = "{";
	jsonString += "\"code\":\"" + getTTSErrorCode(error) + "\",";
	jsonString += "\"description\":\"" + getTTSErrorDesc(error) + "\"";
	jsonString += "}";
	return jsonString;
}

static std::string serializeSNRLevel(double snr)
{
	return "{\"value\":" + toStr(snr) + "}";
}